#pragma once
#include "include/cef_v8.h"

class UV8Handler : public CefV8Handler {

private:
	CefRefPtr<CefV8Context>  callback_context_;
	CefRefPtr<CefV8Value> callback_func_; 
public:
	UV8Handler() {}
 

	virtual CefRefPtr<CefV8Value> getCallback() {
		return callback_func_;
	}

	virtual CefRefPtr<CefV8Context> getCallbackContext() {
		return callback_context_;
	}

	virtual bool Execute(const CefString& name,
		CefRefPtr<CefV8Value> object,
		const CefV8ValueList& arguments,
		CefRefPtr<CefV8Value>& retval,
		CefString& exception) OVERRIDE {
		if (name.compare("myfunc")  == 0 ) {
			// Return my string value.
			retval = CefV8Value::CreateString("My Value!");
			return true;
		} else  if (name.compare("register") == 0 ) {
			if (arguments.size() == 1 && arguments[0]->IsFunction()) {
				callback_func_ = arguments[0].get();
				callback_context_ = CefV8Context::GetCurrentContext();			 
			}
			return true;
		}
		// Function does not exist.
		return false;
	}

	// Provide the reference counting implementation for this class.
	IMPLEMENT_REFCOUNTING(UV8Handler);
};
